import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

public class MyPool {
    //描述一个任务，直接Runnable 不用创建类
    //组织线程
    private BlockingDeque<Runnable> queue=new LinkedBlockingDeque<>();
    //描述一个线程，将任务取出并执行
    static class woker extends  Thread{
        private  BlockingDeque<Runnable> queue=null;

        public woker(BlockingDeque<Runnable> queue) {
            this.queue = queue;
        }
        @Override
        public void run() {
            //循环获取线程池中的任务
            while(true){
                Runnable runnable= null;
                try {
                    runnable = queue.take();
                    //当线程池为空的时候，就会自动发生阻塞。
                    //不为空的时候，会执行任务
                    runnable.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    //创建一个线程来组织线程
    private List<Thread> wokers=new ArrayList<>();
    public MyPool(int n){
        for (int i = 0; i <10 ; i++) {
            woker woker=new woker(queue);
            woker.start();
            wokers.add(woker);
        }
    }
    //创建一个方法，允许吧线程放到线程池出
    public  void submit(Runnable runnable){
        try {
            queue.put(runnable);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        MyPool myPool=new MyPool(10);
        myPool.submit(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <10 ; i++) {
                    System.out.println("hello ");
                }
            }
        });
    }
}
